//////////////////////////////////////////////////////////////////////////////////
//	File	:	GamePlayState.cpp
//	Author	:	Yuri Oyoko
//	Purpose	:	This state defines the main menu state of the game and its behavior
//////////////////////////////////////////////////////////////////////////////////

//Includes
#include "MainMenuState.h"
#include "Definitions.h"
#include "Game.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_WaveManager.h"
#include "SmokeParticleSystem.h"
#include "Particle.h"
#include "Game.h"

SmokeParticleSystem* theSmoke;

MainMenuState::MainMenuState()
{
	BackgroundImageID = -1;
	CursorPos = 0;
	CurrentAlpha = 255;
	//Audio IDs
	BackgroundMusicID = -1;
	MenuFocusSoundID = -1;
	MenuClickSoundID = -1;
	FreeParticles = 0;
	
	PlayChosen = true;
	ExitChosen = false;
	OptionsChosen = false;
	AchievementsChosen = false;
	CreditsChosen = false;
	HelpChosen = false;

}
MainMenuState::~MainMenuState()
{
}
MainMenuState* MainMenuState::GetInstance()
{
	static MainMenuState menuStateInstance;
	return &menuStateInstance;
}

void CreateParticles()
{
	//theSmoke = new SmokeParticleSystem(9);
	//theSmoke->Initialize();
}
void MainMenuState::Enter()
{
	BackgroundImageID = CSGD_TextureManager::GetInstance()->LoadTexture(MainMenuBackgroundImage,0);
	CursorPos = 64;
	PlayChosen = true;
	ExitChosen = false;
	OptionsChosen = false;
	AchievementsChosen = false;
	CreditsChosen = false;
	HelpChosen = false;
	CurrentAlpha = 255;

	MenuFocusSoundID = CSGD_WaveManager::GetInstance()->LoadWave(MenuFocusSound);
	MenuClickSoundID = CSGD_WaveManager::GetInstance()->LoadWave(MenuClickSound);
	BackgroundMusicID = CSGD_WaveManager::GetInstance()->LoadWave(MainMenuMusic);
	
	CSGD_WaveManager::GetInstance()->SetVolume(MenuFocusSoundID, MenuSFXVolume);
	CSGD_WaveManager::GetInstance()->SetVolume(MenuClickSoundID, MenuSFXVolume);
	CSGD_WaveManager::GetInstance()->SetVolume(BackgroundMusicID, MenuMusicVolume);
	CSGD_WaveManager::GetInstance()->Play(BackgroundMusicID,DSBPLAY_LOOPING);
	
	//CreateParticles();

}
void MainMenuState::Exit()
{
	CSGD_TextureManager::GetInstance()->UnloadTexture(BackgroundImageID);

	if(CSGD_WaveManager::GetInstance()->IsWavePlaying(BackgroundMusicID) == true)
		CSGD_WaveManager::GetInstance()->Stop(BackgroundMusicID);
	CSGD_WaveManager::GetInstance()->UnloadWave(BackgroundMusicID);
	CSGD_WaveManager::GetInstance()->UnloadWave(MenuFocusSoundID);
	CSGD_WaveManager::GetInstance()->UnloadWave(MenuClickSoundID);
}
bool MainMenuState::GetInput()
{
	//Arrow keys pressed
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_DOWN))
	{
		CSGD_WaveManager::GetInstance()->Play(MenuFocusSoundID,0);
		if(CursorPos >= 214)
			CursorPos = 64;
		else
			CursorPos += 30;
	}
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_UP))
	{
		CSGD_WaveManager::GetInstance()->Play(MenuFocusSoundID,0);
		if(CursorPos <= 64)
			CursorPos = 214;
		else
			CursorPos -= 30;
	}

	//If Enter key is pressed...
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RETURN))
	{
		CSGD_WaveManager::GetInstance()->Play(MenuClickSoundID,0);
		if(PlayChosen == true)
		{
			//Game::GetInstance()->ChangeState(NULL);
		}
		if(OptionsChosen == true)
		{
			//Game::GetInstance()->ChangeState(NULL);
		}
		if(AchievementsChosen == true)
		{
			//Game::GetInstance()->ChangeState(NULL);
		}
		if(HelpChosen == true)
		{
			//Game::GetInstance()->ChangeState(NULL);
		}
		if(CreditsChosen == true)
		{
			//Game::GetInstance()->ChangeState(NULL);
		}
		if(ExitChosen == true)
		{
			Game::GetInstance()->ChangeState(NULL);
		}
	}

	switch(CursorPos)
	{
		//play chosen
	case 64:
		{
			PlayChosen = true;
			OptionsChosen = false;
			AchievementsChosen = false;
			HelpChosen = false;
			CreditsChosen = false;
			ExitChosen = false;
		}
		break;
		//options chosen
	case 94:
		{
			PlayChosen = false;
			OptionsChosen = true;
			AchievementsChosen = false;
			HelpChosen = false;
			CreditsChosen = false;
			ExitChosen = false;
		}
		break;
		//achievements chosen
	case 124:
		{
			PlayChosen = false;
			OptionsChosen = false;
			AchievementsChosen = true;
			HelpChosen = false;
			CreditsChosen = false;
			ExitChosen = false;
		}
		break;
		//help chosen
	case 154:
		{
			PlayChosen = false;
			OptionsChosen = false;
			AchievementsChosen = false;
			HelpChosen = true;
			CreditsChosen = false;
			ExitChosen = false;
		}
		break;
		//credits chosen
	case 184:
		{
			PlayChosen = false;
			OptionsChosen = false;
			AchievementsChosen = false;
			HelpChosen = false;
			CreditsChosen = true;
			ExitChosen = false;
		}
		break;
		//exit chosen
	case 214:
		{
			PlayChosen = false;
			OptionsChosen = false;
			AchievementsChosen = false;
			HelpChosen = false;
			CreditsChosen = false;
			ExitChosen = true;
		}
		break;
	}
	
	return true;
}
void MainMenuState::Update(float theElapsedTime)
{
	//CurrentAlpha += 5.0f;
	//if(CurrentAlpha >= 255)
	//	CurrentAlpha = 10;

	//theSmoke->AddParticles(D3DXVECTOR2(800,600));
	
	//theSmoke->Update(theElapsedTime);

}
void MainMenuState::Render()
{
	CSGD_Direct3D::GetInstance()->Clear(0,0,0);
	CSGD_Direct3D::GetInstance()->DeviceBegin();

	CSGD_Direct3D::GetInstance()->SpriteBegin();
	CSGD_TextureManager::GetInstance()->Draw(BackgroundImageID,0,0);
	//theSmoke->Render();
	thePrinter.Print("PLAY", 100,64, 0.5f, D3DCOLOR_ARGB(PlayChosen == true ? (int)CurrentAlpha : 100, 255, 255,255));
	thePrinter.Print("OPTIONS", 100,94, 0.5f, D3DCOLOR_ARGB(OptionsChosen == true ? (int)CurrentAlpha : 100, 255, 255,255));
	thePrinter.Print("ACHIEVEMENTS", 100,124, 0.5f, D3DCOLOR_ARGB(AchievementsChosen == true ? (int)CurrentAlpha : 100, 255, 255,255));
	thePrinter.Print("HELP", 100,154, 0.5f, D3DCOLOR_ARGB(HelpChosen == true ? (int)CurrentAlpha : 100, 255, 255,255));
	thePrinter.Print("CREDITS", 100,184, 0.5f, D3DCOLOR_ARGB(CreditsChosen == true ? (int)CurrentAlpha : 100, 255, 255,255));
	thePrinter.Print("EXIT", 100,214, 0.5f, D3DCOLOR_ARGB(ExitChosen == true ? (int)CurrentAlpha : 100, 255, 255,255));
	
	CSGD_Direct3D::GetInstance()->SpriteEnd();
	
	CSGD_Direct3D::GetInstance()->DrawTextA("Left 4 Dead: 2D Edition Tech Demo", 530, 580, 255,0,0);
	CSGD_Direct3D::GetInstance()->DrawTextA(Game::GetInstance()->GetFPS(), 10,10,255,0,0);
	//char cc[128];
	//sprintf(cc, "Free Particle Count: %i", FreeParticles);
	//CSGD_Direct3D::GetInstance()->DrawTextA(cc, 10,30,255,0,0);
    CSGD_Direct3D::GetInstance()->SpriteBegin();

	
	CSGD_Direct3D::GetInstance()->SpriteEnd();
    CSGD_Direct3D::GetInstance()->DeviceEnd();
	CSGD_Direct3D::GetInstance()->Present();
	
}